package com.gyg.schedule;

import cn.hutool.core.collection.CollectionUtil;
import com.gyg.domain.*;
import com.gyg.service.*;
import com.gyg.utils.OSSUtil;
import com.gyg.utils.TimeUtilCustomize;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
@Slf4j
public class OSSCleanJob {

    @Autowired
    private CloudUserService cloudUserService;

    @Autowired
    private CloudMessageService cloudMessageService;

    @Autowired
    private CloudPictureService cloudPictureService;

    @Autowired
    private CloudDocumentService cloudDocumentService;

    @Autowired
    private CloudAudioService cloudAudioService;

    @Autowired
    private CloudVideoService cloudVideoService;

    @Autowired
    private OSSUtil ossUtil;

    /**
     * 每天凌晨1点10分清理 阿里云OSS服务器 cloud/head/文件夹下的垃圾图片
     */
    @Scheduled(cron = "0 10 1 * * ?")
    public void cloudHeadOSSCleanJob() {
        log.info("每天凌晨1点10分清理阿里云OSS服务器cloud/head/文件夹下的垃圾图片任务开始...");
        List<CloudUser> cloudUserList = cloudUserService.list();

        if (CollectionUtil.isEmpty(cloudUserList)) {
            return;
        }

        //所有照片url集合
        List<String> urlList = cloudUserList.stream()
                .map(CloudUser::getPicPath)
                .collect(Collectors.toList());


        List<String> stringList = ossUtil.list("cloud/head");

        cleanOSS(urlList, stringList, "cloud/head/");
        log.info("每天凌晨1点10分清理阿里云OSS服务器cloud/head/文件夹下的垃圾图片任务结束...");
    }

    /**
     * 每天凌晨1点20分清理 阿里云OSS服务器 cloud/message/文件夹下的垃圾图片
     */
    @Scheduled(cron = "0 20 1 * * ?")
    public void cloudMessageOSSCleanJob() {
        log.info("每天凌晨1点20分清理阿里云OSS服务器cloud/message/文件夹下的垃圾图片任务开始...");
        List<CloudMessage> cloudMessageList = cloudMessageService.list();

        if (CollectionUtil.isEmpty(cloudMessageList)) {
            return;
        }

        List<String> urlList = cloudMessageList.stream()
                .map(CloudMessage::getMessageUrl)
                .collect(Collectors.toList());

        List<String> stringList = ossUtil.list("cloud/message");

        cleanOSS(urlList, stringList, "cloud/message/");
        log.info("每天凌晨1点20分清理阿里云OSS服务器cloud/message/文件夹下的垃圾图片任务结束...");
    }

    /**
     * 每天凌晨1点30分清理 阿里云OSS服务器 cloud/picture/文件夹下的垃圾图片
     */
    @Scheduled(cron = "0 30 1 * * ?")
    public void cloudPictureOSSCleanJob() {
        log.info("每天凌晨1点30分清理阿里云OSS服务器cloud/picture/文件夹下的垃圾图片任务开始...");
        List<CloudPicture> cloudPictureList = cloudPictureService.list();

        if (CollectionUtil.isEmpty(cloudPictureList)) {
            return;
        }

        List<String> urlList = cloudPictureList.stream()
                .map(CloudPicture::getPictureUrl)
                .collect(Collectors.toList());

        List<String> stringList = ossUtil.list("cloud/picture");

        cleanOSS(urlList, stringList, "cloud/picture/");
        log.info("每天凌晨1点30分清理阿里云OSS服务器cloud/picture/文件夹下的垃圾图片任务结束...");
    }

    /**
     * 每天凌晨1点40分清理 阿里云OSS服务器 cloud/document/文件夹下的垃圾图片
     */
    @Scheduled(cron = "0 40 1 * * ?")
    public void cloudDocumentOSSCleanJob() {
        log.info("每天凌晨1点40分清理阿里云OSS服务器cloud/document/文件夹下的垃圾文档任务开始...");
        List<CloudDocument> cloudDocumentList = cloudDocumentService.list();

        if (CollectionUtil.isEmpty(cloudDocumentList)) {
            return;
        }

        List<String> urlList = cloudDocumentList.stream()
                .map(CloudDocument::getDocumentUrl)
                .collect(Collectors.toList());

        List<String> stringList = ossUtil.list("cloud/document");

        cleanOSS(urlList, stringList, "cloud/document/");
        log.info("每天凌晨1点40分清理阿里云OSS服务器cloud/document/文件夹下的垃圾文档任务结束...");
    }

    /**
     * 每天凌晨1点50分清理 阿里云OSS服务器 cloud/audio/文件夹下的垃圾音频
     */
    @Scheduled(cron = "0 50 1 * * ?")
    public void cloudAudioOSSCleanJob() {
        log.info("每天凌晨1点50分清理阿里云OSS服务器cloud/audio/文件夹下的垃圾音频任务开始...");
        List<CloudAudio> cloudAudioList = cloudAudioService.list();

        if (CollectionUtil.isEmpty(cloudAudioList)) {
            return;
        }

        List<String> urlList = cloudAudioList.stream()
                .map(CloudAudio::getAudioUrl)
                .collect(Collectors.toList());

        List<String> stringList = ossUtil.list("cloud/audio");

        cleanOSS(urlList, stringList, "cloud/audio/");
        log.info("每天凌晨1点50分清理阿里云OSS服务器cloud/audio/文件夹下的垃圾音频任务结束...");
    }

    /**
     * 每天凌晨2点00分清理 阿里云OSS服务器 cloud/video/文件夹下的垃圾视频
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void cloudVideoOSSCleanJob() {
        log.info("每天凌晨2点00分清理阿里云OSS服务器cloud/video/文件夹下的垃圾视频任务开始...");
        List<CloudVideo> cloudVideoList = cloudVideoService.list();

        if (CollectionUtil.isEmpty(cloudVideoList)) {
            return;
        }

        List<String> urlList = cloudVideoList.stream()
                .map(CloudVideo::getVideoUrl)
                .collect(Collectors.toList());

        List<String> stringList = ossUtil.list("cloud/video");

        cleanOSS(urlList, stringList, "cloud/video/");
        log.info("每天凌晨2点00分清理阿里云OSS服务器cloud/video/文件夹下的垃圾视频任务结束...");
    }


    /**
     * 阿里云OSS文件清理 公用方法
     * @param urlList
     * @param stringList
     * @param objectName
     */
    private void cleanOSS(List<String> urlList, List<String> stringList, String objectName) {
        //时间
        String date = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_DAY_FORMAT, new Date());

        if (Objects.isNull(stringList) || stringList.isEmpty()) {
            log.info(date + objectName + "文件夹下暂无垃圾文件清理");
            return;
        }

        stringList = stringList.stream()
                .filter(v -> !StringUtils.equals(v, objectName))
                .map(v -> "http://gaoyiguan.cn/" + v)
                .collect(Collectors.toList());

        if (Objects.isNull(stringList) || stringList.isEmpty()) {
            log.info(date + objectName + "文件夹下暂无垃圾文件清理");
            return;
        }

        //两个集合取差集
        stringList.removeAll(urlList);

        if (Objects.isNull(stringList) || stringList.isEmpty()) {
            log.info(date + objectName + "文件夹下暂无垃圾文件清理");
            return;
        }

        stringList = stringList.stream()
                .map(v -> {
                    return v.split("http://gaoyiguan.cn/")[1];
                })
                .collect(Collectors.toList());

        ossUtil.deleteAll(stringList);
        log.info(date + objectName + "文件夹下垃圾文件清理完毕！");
    }

}
